package com.siderakis.gwt.cellwidgets.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.RootPanel;
import com.siderakis.gwt.cellwidgets.shared.HasColor;
import com.siderakis.gwt.cellwidgets.shared.HasPercentage;

/**
 * 
 * @author Nick Siderakis
 * 
 * */
public class GWT_Cell_Widgets implements EntryPoint {
	private interface Obj extends HasPercentage, HasColor {

	}

	private CellTable<Obj> createCellTable() {
		final CellTable<Obj> cellTable = new CellTable<Obj>();
		final StatusCell<HasPercentage> statusCell = new StatusCell<HasPercentage>(
				new StatusCell.Delegate<HasPercentage>() {
					@Override
					public void execute(final HasPercentage object) {
						History.newItem("show:" + object.getPercentage());
					}
				});

		final Column<Obj, HasPercentage> statusColumn = new Column<Obj, HasPercentage>(
				statusCell) {
			@Override
			public HasPercentage getValue(final Obj row) {
				return row;
			}
		};

		final ColorCell colorCell = new ColorCell();

		final Column<Obj, HasColor> colorColumn = new Column<Obj, HasColor>(
				colorCell) {
			@Override
			public HasColor getValue(final Obj row) {
				return row;
			}
		};

		cellTable.addColumn(statusColumn, "Status");
		cellTable.addColumn(colorColumn, "Color");
		cellTable.setWidth("200px");
		return cellTable;
	};

	private List<Obj> generateData() {
		final List<Obj> data = new ArrayList<Obj>();
		for (int i = 0; i < 100; i++) {
			data.add(new Obj() {
				@Override
				public String getColor() {
					return (Random.nextBoolean() ? "00" : "FF")
					+ (Random.nextBoolean() ? "00" : "FF")
					+ (Random.nextBoolean() ? "00" : "FF");
				}

				@Override
				public Integer getPercentage() {
					return Random.nextInt(100);
				}
			});
		}
		return data;
	}

	@Override
	public void onModuleLoad() {
		final List<Obj> data = generateData();
		final CellTable<Obj> cellTable = createCellTable();

		cellTable.setRowData(0, data);

		RootPanel.get("demo").add(cellTable);

	}

}
